package cc.spider.web;

import cc.spider.core.queue.PriorityQueue;
import cc.spider.model.SpiderConfig;
import cc.spider.web.config.Config;
import cc.spider.web.datapipes.ConsoleDataPipe;
import cc.spider.web.fetcher.OkHttpFetcher;
import cc.spider.web.model.GlobalDefaultDataExt;
import cc.spider.web.processor.UnifiedDataProcessor;
import com.alibaba.fastjson.JSON;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class WebSpiderExtConfig extends SpiderConfig {
    public static Config config;

    public WebSpiderExtConfig(String fileName) {
        String cfgStr = WebSpiderExtConfig.getResourcesFileContent(fileName);
        WebSpiderExtConfig.config = JSON.parseObject(cfgStr, Config.class);
        this.init();
    }

    private void init() {
        this.setFetcher(new OkHttpFetcher());
        this.setDataProcess(new UnifiedDataProcessor());
        this.addDataPipe(new ConsoleDataPipe());
        this.setGlobalDefaultData(new GlobalDefaultDataExt());
        this.setTaskQueue(new PriorityQueue());
        this.setSleepTime(2000);
    }

    private static String getResourcesFileContent(String path) {
        try {
            InputStream ips = WebSpiderExtConfig.class.getClassLoader().getResourceAsStream(path);
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int eof;
            while ((eof = ips.read(buffer)) != -1) {
                baos.write(buffer, 0, eof);
            }
            baos.close();
            ips.close();
            return baos.toString(StandardCharsets.UTF_8.toString());
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

}
